package com.ukao.cashregister.utils;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;


/**
 * Created by ukaodev on 2017/5/31.
 */

public class StackManager {




    private   StackManager(){}

    public static     class   SingletonInstance {
        private static final StackManager INSTANCE = new StackManager();
    }

    public static synchronized  StackManager getInstance() {
        return SingletonInstance.INSTANCE;
}






    public Fragment getFragment( FragmentActivity context ,String tag){

     return context.getSupportFragmentManager().findFragmentByTag(tag);
    }


    public void setFragment(FragmentActivity context ,Fragment mTargetFragment,int layoutId){

        FragmentTransaction transaction = context.getSupportFragmentManager().beginTransaction();
        transaction
                .replace(layoutId, mTargetFragment,mTargetFragment.getClass().getName())
                .setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
                .commit();
    }

    // 弹出页面
    public void popFragmant(FragmentActivity context ,Fragment from, Fragment to,int layoutId) {

        FragmentTransaction transaction = context.getSupportFragmentManager().beginTransaction();
        transaction.hide(from)
                .add(layoutId, to, to.getClass().getName())
                .addToBackStack(to.getClass().getName())
                .commit();
    }

    /**
     * 关闭当前页面回到指定页面
     * @param from 当前页面
     * @param tag 指定页面tag
     */
    public void popFragmant(FragmentActivity context ,Fragment from,String tag) {

        FragmentTransaction transaction = context.getSupportFragmentManager().beginTransaction();
        Fragment to = context.getSupportFragmentManager().findFragmentByTag(tag);
        transaction.remove(from)
                .show(to)
                .addToBackStack(to.getClass().getName())
                .commit();
        context.getSupportFragmentManager().popBackStack();
    }

    // 弹出页面， dialog 形式
    public void popFragmant(FragmentActivity context ,Fragment to,int layoutId) {

        FragmentTransaction transaction = context.getSupportFragmentManager().beginTransaction();
        if (!to.isAdded()) {
            transaction
                    .add(layoutId, to, to.getClass().getName())
                    .addToBackStack(to.getClass().getName())
                    .commit();
        }
    }

    // 关闭页面
    public void closeFragment(FragmentActivity context ,Fragment mTargetFragment){

        FragmentTransaction transaction = context.getSupportFragmentManager().beginTransaction();
        transaction
                .remove(mTargetFragment)
                .commit();
        context.getSupportFragmentManager().popBackStack();
    }

    // 关闭所有页面
    public void closeAllFragment(FragmentActivity context ){
;
        int backStackCount =  context.getSupportFragmentManager().getBackStackEntryCount();
        for (int i = 0; i < backStackCount; i++) {
            int backStackId =  context.getSupportFragmentManager().getBackStackEntryAt(i).getId();
            context.getSupportFragmentManager().popBackStack(backStackId, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        }
    }



}
